package main.java.DMDE.persistence;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import main.java.DMDE.model.*;
import main.java.DMDE.model.Class;
import main.java.DMDE.model.relationships.Relationship;
import main.java.common.IXmlManager;

public class DiagramXmlManager implements IXmlManager<Diagram> {

	private ClassXmlManager classManager = new ClassXmlManager();
	private InterfaceXmlManager interfaceManager = new InterfaceXmlManager();
	private RelationshipXmlManager relationshipManager = new RelationshipXmlManager();
	
	public Element getElementFromItem(Diagram diagram, Document document) {
		Element diagramElement = document.createElement("diagram");
		relationshipManager.currentDiagram = diagram;
		diagramElement.setAttribute("name", diagram.getName());
		diagramElement.setAttribute("type", "dm");
		Element entities = document.createElement("entities");
		for (Entity entity : diagram.getEntities()) {
			Element element = null;
			if (entity instanceof Class) {
				element = classManager.getElementFromItem(entity, document);
			}
			else if (entity instanceof Interface) {
				element = interfaceManager.getElementFromItem(entity, document);
			}
			else {
				continue;
			}
			entities.appendChild(element);
		}
		diagramElement.appendChild(entities);
		Element relationships = document.createElement("relationships");
		for (Relationship relationship : diagram.getRelationships()) {
			relationships.appendChild(relationshipManager.getElementFromItem(relationship, document));
		}
		diagramElement.appendChild(relationships);
		Element subdiagrams = document.createElement("subdiagrams");
		for (Diagram subDiagram : diagram.getSubDiagrams()) {
			subdiagrams.appendChild(getElementFromItem(subDiagram, document));
		}
		diagramElement.appendChild(subdiagrams);
		return diagramElement;
	}

	@Override
	public Diagram getItemFromXmlElement(Element element) throws Exception {
		Diagram diagram = new Diagram();
		relationshipManager.currentDiagram = diagram;
		diagram.setName(element.getAttribute("name"));
		NodeList entities = element.getElementsByTagName("entities").item(0).getChildNodes();
		for (int i = 0; i < entities.getLength(); i++) {
			Node node = entities.item(i);
			if (node == null || !(node instanceof Element))
				continue;
			Element entityElement = (Element) node;
			Entity entity = null;
			if (entityElement.getNodeName() == "class") {
				entity = classManager.getItemFromXmlElement(entityElement);
			}
			else if (entityElement.getNodeName() == "interface") {
				entity = interfaceManager.getItemFromXmlElement(entityElement);
			}
			else {
				continue;
			}
			diagram.addEntity(entity);
		}
		NodeList relationships = element.getElementsByTagName("relationships").item(0).getChildNodes();
		for (int i = 0; i < relationships.getLength(); i++) {
			Node node = relationships.item(i);
			if (node == null || !(node instanceof Element))
				continue;
			Element relationshipElement = (Element) node;
			diagram.addRelationship(relationshipManager.getItemFromXmlElement(relationshipElement));
		}
		NodeList subdiagrams = element.getElementsByTagName("subdiagrams").item(0).getChildNodes();
		for (int i = 0; i < subdiagrams.getLength(); i++) {
			Node node = subdiagrams.item(i);
			if (node == null || !(node instanceof Element))
				continue;
			Element subdiagramElement = (Element) node;
			diagram.addSubDiagram(getItemFromXmlElement(subdiagramElement));
		}
		return diagram;
	}
}
